//@angular/core是一个npm的模块，定义了angular的核心功能
import { Component } from '@angular/core';

import { Http, Response } from '@angular/http';

//导入http请求的工具

//NavController就是用来管理和导航页面的一个controlle
//NavParams 这个是参数
import { NavController,NavParams } from 'ionic-angular';

import {ToastController  } from 'ionic-angular';
@Component({
    selector: 'page-web',
    templateUrl: 'refresh.html'
  })
//export关键词：将类暴露出去，以便其它的文件可以import到它。
//类的命名：在ionic3中，页面类采用页面名+Page的命名方式，首字母大写，如HomePage, ContactPage等。
export class RefreshPage {

    users:any = [{name:'小明',age:'1970', gender:'1', description:'逗比',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'小王',age:'1992', gender:'0', description:'王逗比',img:"http://www.runoob.com/try/demo_source/spengler.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/stantz.jpg"},
                ];

    user2:any = [{name:'yellowcong',age:'1970', gender:'1', description:'逗比',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1992', gender:'0', description:'王逗比',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                ];

    default_data:any = [{name:'yellowcong',age:'1970', gender:'1', description:'逗比',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1992', gender:'0', description:'王逗比',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                {name:'yellowcong',age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"},
                ];
    //当前日期
    year_now;
    
    //条数
    cnt:number =0;

    constructor(private toastCtrl:ToastController){
        this.year_now = new Date().getFullYear();
    }

    //下滑动加载数据
    doInfinite(infiniteScroll){

        setTimeout(() => { 
            console.log('加载完成后，关闭刷新'); 
            infiniteScroll.complete();
            
            //toast提示
            this.showInfo("加载成功");
            //增加index
            this.cnt ++;
            var data1 = {name:'yellowcong'+this.cnt,age:'1994', gender:'1', description:'三炮',img:"http://www.runoob.com/try/demo_source/venkman.jpg"};
            this.users.push(data1);

            
            if(this.cnt >=10){
                //如果都加载完成的情况，就直接 disable ，移除下拉加载
                infiniteScroll.enable(false);
                //toast提示
                this.showInfo("已加载所有");
            }
        }, 2000);
    }

    //下拉刷型界面
    doRefresh(refresher){

        console.log("下拉刷新");
        //动态切换
        //当用户数量为3
        if(this.users.length ==3){
            this.users = this.user2;
        }else{
            this.users = this.default_data;
        }
        setTimeout(() => { 
            console.log('加载完成后，关闭刷新'); 
            refresher.complete();
            
            //toast提示
            this.showInfo("加载成功");
        }, 2000);
    }


     //点击了哪一条消息
     clickItem(item){
        console.log(item);
        this.showInfo('点击了'+ item.name);
    }

    //显示toast消息
    showInfo(msg){
        let toast = this.toastCtrl.create({
            message: msg, //提示消息
            duration: 3000,//3秒后自动消失
            position: 'bottom',//位置top,bottom
            showCloseButton:true, //是否显示关闭按钮
            closeButtonText:"关闭" //关闭按钮字段
        });

        //关闭后执行的操作
        toast.onDidDismiss(() => { console.log('toast被关闭之后执行'); });

        //显示toast
        toast.present();//符合触发条件后立即执行显示。
    }
}